/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import vo.PropertiesVO;
import vo.TransactionToReportVO;
import ws.report.Reports;
import ws.report.Reports_Service;
import ws.report.TransactionVO;

/**
 *
 * @author julianDavidPG
 */
public class ServiceControl {
    
    private static final String HOST = "REPORT_HOST";
    private static final String PORT = "REPORT_PORT";
    private static final String RESOURCE = "REPORT_RESOURCE";
    private String serviceLocation;
    private URL url = null;

    public ServiceControl() {
        try {
            PropertiesControl.setHOST(HOST);
            PropertiesControl.setPORT(PORT);
            PropertiesControl.setRESOURCE(RESOURCE);
            PropertiesVO p = PropertiesControl.localProperties();
            serviceLocation = "http://" + p.getHost() + ":" + p.getPort().toString() + p.getResource();
            url = new URL(serviceLocation);
        } catch (MalformedURLException malformedURLException) {
            System.out.println("direccion invalida");
        } catch (IOException e) {
            System.out.println("ARCHIVO PROPERTIES FALLO");
            serviceLocation = "http://localhost:8080/Reports/Reports?WSDL";
            try {
                url = new URL(serviceLocation);
            } catch (MalformedURLException ex) {
                System.out.println("direccion invalida");
                Logger.getLogger(LoginServiceControl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void fillMongo() {
        Reports_Service service = new Reports_Service(url);
        Reports port = service.getReportsPort();
        port.fillMongo();
    }

    public List<String> getReportTypes() {
        Reports_Service service = new Reports_Service(url);
        Reports port = service.getReportsPort();
        return port.getReportTypes();
    }

    public List<TransactionToReportVO> getTransactions(String initialDate, String finalDate, String establishmentID, String brandID, 
                                                String franchiseID, String reportType, double initAmount, double finAmount) {
        Reports_Service service = new Reports_Service(url);
        Reports port = service.getReportsPort();
        List<TransactionVO> list = port.getTransactions(initialDate, finalDate, establishmentID, brandID, franchiseID, reportType, initAmount, finAmount);
        ArrayList<TransactionToReportVO> ret = new ArrayList<>();
        for (TransactionVO t : list) {
            ret.add(new TransactionToReportVO(t.getDate(), t.getEstablishmentId(), String.valueOf(t.getAmount()), t.getFranchiseId(), 
                    t.getDNI(), String.valueOf(t.getValidationResult()), t.getPurchaseDetail()));
        }
        return ret;
    }
    
    
}

